package com.example.caimogu.Goods2Json;

import com.example.caimogu.bean.Goods;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringWriter;


public class PicDataManager {
	private Goods goods;



	public void test11(){
		//file2String(, " ");
		//string2File(s, "sss","id1","pic");

	}
	
	public boolean string2File(String res,String bid,String gid, String filePath) {
		boolean flag = true;
		try {
			File distFile = new File("C:/GPicture/"+"/"+bid+"/"+gid+"/"+filePath+".png");
			if (!distFile.getParentFile().exists())
				distFile.getParentFile().mkdirs();

			InputStream in = new ByteArrayInputStream(res.getBytes("ISO-8859-1"));
			OutputStream o = new FileOutputStream(distFile);

			byte buf[] = new byte[1024]; // �ַ�������
			int len;
			while ((len = in.read(buf)) != -1) {
				o.write(buf, 0, len);
			}
			o.flush();
			o.close();
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
			flag = false;
			return flag;
		}
		return flag;
	}
	
	public String file2String(File file, String encoding) throws IOException {
		 InputStreamReader reader = null;
        StringWriter writer = new StringWriter(); 
        try { 
                if (encoding == null || "".equals(encoding.trim())) {
                        reader = new InputStreamReader(new FileInputStream(file),"ISO-8859-1"); 
                } else { 
                        reader = new InputStreamReader(new FileInputStream(file), encoding); 
                } 

                char[] buffer = new char[1024]; 
                int n = 0; 
                while (-1 != (n = reader.read(buffer))) { 
                        writer.write(buffer, 0, n); 
                } 
        } catch (Exception e) { 
                e.printStackTrace(); 
                return null; 
        } finally { 
                if (reader != null) 
                        try { 
                                reader.close(); 
                        } catch (IOException e) { 
                                e.printStackTrace(); 
                        } 
        } 

        if (writer != null) 
                return writer.toString(); 
        else 
        	return null; 
}
}

	
